/**
 * 文件：QueryCoursePanel.java
 * 说明：用于查询学生
 * 作者：古通基
 * 时间：1211-09-13
 */

package com.java.component;

import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class QueryCoursePanel extends JPanel implements ActionListener,MouseListener
{
	private static final long serialVersionUID = -8016727800725988973L;
	private JLabel cno = new JLabel("课  程  号：");                                   //静态标签
	private JLabel cname = new JLabel("课  程  名：");
	private JLabel cteacher = new JLabel("授课老师：");             
	private JLabel ccredit = new JLabel("学        分：");        
	private JLabel ctime = new JLabel("课        时：");       
	private JLabel caddress = new JLabel("上课地点：");         
	
	private JTextField no = new JTextField("请输入课程号",10);                  //文本输入
	private JTextField name = new JTextField("",10); 
	private JTextField teacher = new JTextField("",10); 
	private JTextField credit= new JTextField("",10);  
	private JTextField time = new JTextField("",10);  
    private JTextField address = new JTextField("",10);  
	
    private JButton query = new JButton("查询");                            //声明按钮
    private JButton reset = new JButton("重置");
    	
    private JPanel JPcomon1 = new JPanel();                                  //声明面板
    private JPanel JPcomon2 = new JPanel();
    private JPanel JPcomon3 = new JPanel();
    private JPanel JPcomon4 = new JPanel();
    private JPanel JPcomon5 = new JPanel();
    private JPanel JPcomon6 = new JPanel();
    private JPanel JPbutton = new JPanel();
    
    public QueryCoursePanel()                                                        //构造函数
    {
    	cno.setHorizontalAlignment(SwingConstants.CENTER);
    	cname.setHorizontalAlignment(SwingConstants.CENTER);
    	cteacher.setHorizontalAlignment(SwingConstants.CENTER);
    	ccredit.setHorizontalAlignment(SwingConstants.CENTER);
    	ctime.setHorizontalAlignment(SwingConstants.CENTER);
    	caddress.setHorizontalAlignment(SwingConstants.CENTER);
    	
    	JPcomon1.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon1.add(cno);
    	JPcomon1.add(no);
    	JPcomon2.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon2.add(cname);
    	JPcomon2.add(name);
    	JPcomon3.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon3.add(cteacher);
    	JPcomon3.add(teacher);
    	JPcomon4.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon4.add(ccredit);
    	JPcomon4.add(credit);
    	JPcomon5.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon5.add(ctime);
    	JPcomon5.add(time);
    	JPcomon6.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon6.add(caddress);
    	JPcomon6.add(address);
    	
    	JPbutton.setLayout(new FlowLayout(FlowLayout.CENTER,12,5));
    	JPbutton.add(query);
    	JPbutton.add(reset);
    	
    	this.setLayout(new GridLayout(10,1));
    	this.add(JPcomon1);
    	this.add(JPcomon2);
    	this.add(JPcomon3);
    	this.add(JPcomon4);
    	this.add(JPcomon5);
    	this.add(JPcomon6);
    	this.add(JPbutton);
    	
    	query.addActionListener(this);                                                        //事件监听        
    	reset.addActionListener(this);
    	no.addMouseListener(this);
    }
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand().equals("查询"))
		{
			QueryDataBase();
			name.setEditable(false);
			teacher.setEditable(false);
			credit.setEditable(false);
			time.setEditable(false);
			address.setEditable(false);
			no.setEditable(false);
		}
		else if(e.getActionCommand().equals("重置"))
		{
			no.setText("");
			name.setText("");
			teacher.setText("");
			credit.setText("");
			time.setText("");
			address.setText("");
			no.setEditable(true);
		}
	}		
	private void QueryDataBase()
	{
		ResultSet rs = null;
		try
		{
			BaseSQL course = null;
			course = new BaseSQL();
			String sql = "select * from course where cno = '"+Integer.parseInt(no.getText())+"'"; 
			
		    rs = course.executeSelect(sql);
		    if(rs.next())
    		{
    			if(Integer.parseInt( no.getText())==rs.getInt("cno"))
    			{
    				name.setText(rs.getString("cname"));
    				teacher.setText(rs.getString("teacher"));
    				credit.setText(" "+rs.getString("credit")+" ");
    				time.setText(rs.getString("time"));
    				address.setText(rs.getString("address"));	
        		}    				
       		} 
    		else
    		{
    			JOptionPane.showMessageDialog(null,"不存在课程号为："+no.getText()+" 的课程","课程查询",JOptionPane.INFORMATION_MESSAGE);
    		}
		}
		catch(SQLException evt)
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		} 
		catch (ClassNotFoundException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}	
		catch (NumberFormatException evt)  
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
	}
	
    public void mouseClicked(MouseEvent e) { }                                           //鼠标事件
    public void mouseReleased(MouseEvent e) { }
    public void mouseEntered(MouseEvent e) 
    {
    	if(no.getText().equals("请输入课程号"))
    	{    		
    		no.setText("");
    		this.setVisible(true);
    	}    	
    }
    public void mousePressed(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }
}